﻿using System;
using System.Dynamic;
using System.Net;
using System.Text;
using System.Web;
using System.Web.Mvc;
using System.Web.Script.Serialization;
using Microsoft.Practices.ServiceLocation;
using OMR.Crawler.Core;
using SolrNet;
using SolrNet.Commands.Parameters;

namespace OMR.Crawler.SampleMVC.Controllers
{
    public class HomeController : Controller
    {
        ISolrOperations<CrawlDocument> solr;

        public HomeController()
        {
            solr = ServiceLocator.Current.GetInstance<ISolrOperations<CrawlDocument>>();
            //solr.BuildSpellCheckDictionary();
        }

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Search(string q, int? p)
        {
            @ViewBag.q = q;
            @ViewBag.start = p.HasValue && p.Value > 0 ? p - 1 : 0;

            if (q.IndexOf(":") < 0)
                q = "Content:" + q;

            var sq = new SolrQuery(q);

            var opt = new QueryOptions();
            opt.Highlight = new HighlightingParameters();
            //opt.Fields.Add("Title");
            opt.Highlight.Fields = new string[] { "Content" };

            opt.AddFields("Title", "ContentType", "Location", "Size");
            opt.Highlight.Fragsize = 350;
            opt.Highlight.HighlightMultiTerm = true;

            opt.Rows = 10;
            opt.Start = @ViewBag.start;

            opt.SpellCheck = new SpellCheckingParameters { Collate = true };

            var query = solr.Query(sq, opt);

            return View(query);
        }

    }

    public static class MyHtmlHelpers
    {
        public static string Pager(this HtmlHelper helper, int currentPage, int currentPageSize, int totalRecords, string urlPrefix)
        {
            StringBuilder sb1 = new StringBuilder();

            int seed = currentPage % currentPageSize == 0 ? currentPage : currentPage - (currentPage % currentPageSize);

            if (currentPage > 0)
                sb1.AppendLine(String.Format("<a href=\"{0}&p={1}\">Previous</a>", urlPrefix, currentPage));

            if (currentPage - currentPageSize >= 0)
                sb1.AppendLine(String.Format("<a href=\"{0}&p={1}\">...</a>", urlPrefix, (currentPage - currentPageSize) + 1));

            for (int i = seed; i < Math.Round((totalRecords / 10) + 0.5) && i < seed + currentPageSize; i++)
            {
                sb1.AppendLine(String.Format("<a href=\"{0}&p={1}\">{1}</a>", urlPrefix, i + 1));
            }

            if (currentPage + currentPageSize <= (Math.Round((totalRecords / 10) + 0.5) - 1))
                sb1.AppendLine(String.Format("<a href=\"{0}&p={1}\">...</a>", urlPrefix, (currentPage + currentPageSize) + 1));

            if (currentPage < (Math.Round((totalRecords / 10) + 0.5) - 1))
                sb1.AppendLine(String.Format("<a href=\"{0}&p={1}\">Next</a>", urlPrefix, currentPage + 2));

            return sb1.ToString();
        }
    }

}


